package com.hongyun.tms.infra.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.hongyun.tms.common.enums.BizCode;
import com.hongyun.tms.exception.BusinessRuntimeException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 通用日期转换器，支持LocalDate和LocalDateTime
 *
 * @author peng.li
 * @date 2024-09-12
 */
public class LocalDateTimeOrDateConverter implements Converter<Object> {

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    @Override
    public Class supportJavaTypeKey() {
        return Object.class;  // 支持任意类型
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        WriteCellData<String> cellData = new WriteCellData<>();
        String cellValue;

        if (value instanceof LocalDateTime) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            cellValue = formatter.format((LocalDateTime) value);
        } else if (value instanceof LocalDate) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            cellValue = formatter.format((LocalDate) value);
        } else {
            throw new BusinessRuntimeException(BizCode.EXCEL_ERROR, "导出时处理时间相关的数据异常");
        }

        cellData.setType(CellDataTypeEnum.STRING);
        cellData.setStringValue(cellValue);
        return cellData;
    }

    @Override
    public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String cellValue = cellData.getStringValue();
        int rowIndex = cellData.getRowIndex() + 1; // 行号从1开始计数
        int columnIndex = cellData.getColumnIndex() + 1; // 列号从1开始计数

        try {
            // 优先尝试转换为 LocalDateTime
            return LocalDateTime.parse(cellValue, DATE_TIME_FORMATTER);
        } catch (Exception e) {
            try {
                // 尝试转换为 LocalDate
                return LocalDate.parse(cellValue, DATE_FORMATTER);
            } catch (Exception ex) {
                // 捕获转换失败的异常，并提示具体的行列号及输入的值
                throw new BusinessRuntimeException(BizCode.EXCEL_ERROR,
                        String.format("第%d行第%d列日期格式解析错误: 输入值 [%s]", rowIndex, columnIndex, cellValue));
            }
        }
    }
}
